/*
Copyright (c) 2008, Oleg S. Estekhin
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice,
      this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.
    * Neither the name of the copyright holders nor the names of its
      contributors may be used to endorse or promote products derived from this
      software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package ebml.viewer;

import java.io.File;

import ebml.ElementDescriptor;
import ebml.ElementType;
import ebml.VInteger;
import ebml.viewer.table.ExtendedTableModel;

/** Manages the element statistics. */
public final class ElementStatsTableModelProvider implements FileTaskListener {

    private final ElementStatsTableModel tableModel;


    /** Creates a new <code>ElementTableModelProvider</code> object. */
    public ElementStatsTableModelProvider() {
        tableModel = new ElementStatsTableModel();
    }


    /**
     * Returns the <code>TableModel</code> constructed by this provider.
     *
     * @return the table model
     */
    public ExtendedTableModel getTableModel() {
        return tableModel;
    }


    private void processElement(VInteger identifier, String name, ElementType type, VInteger size) {
        tableModel.processElement(identifier, name, type, size);
    }


    @Override
    public void startFile(File file, long size) {
    }

    @Override
    public void signedIntegerElement(long position, ElementDescriptor descriptor, VInteger size, long value) {
        processElement(descriptor.getIdentifier(), descriptor.getName(), descriptor.getType(), size);
    }

    @Override
    public void unsignedIntegerElement(long position, ElementDescriptor descriptor, VInteger size, long value) {
        processElement(descriptor.getIdentifier(), descriptor.getName(), descriptor.getType(), size);
    }

    @Override
    public void floatElement(long position, ElementDescriptor descriptor, VInteger size, Number value) {
        processElement(descriptor.getIdentifier(), descriptor.getName(), descriptor.getType(), size);
    }

    @Override
    public void asciiStringElement(long position, ElementDescriptor descriptor, VInteger size, String value) {
        processElement(descriptor.getIdentifier(), descriptor.getName(), descriptor.getType(), size);
    }

    @Override
    public void unicodeStringElement(long position, ElementDescriptor descriptor, VInteger size, String value) {
        processElement(descriptor.getIdentifier(), descriptor.getName(), descriptor.getType(), size);
    }

    @Override
    public void dateElement(long position, ElementDescriptor descriptor, VInteger size, long value) {
        processElement(descriptor.getIdentifier(), descriptor.getName(), descriptor.getType(), size);
    }

    @Override
    public void binaryElement(long position, ElementDescriptor descriptor, VInteger size) {
        processElement(descriptor.getIdentifier(), descriptor.getName(), descriptor.getType(), size);
    }

    @Override
    public void startContainer(long position, ElementDescriptor descriptor, VInteger size) {
        processElement(descriptor.getIdentifier(), descriptor.getName(), descriptor.getType(), size);
    }

    @Override
    public void endContainer(long position, ElementDescriptor descriptor) {
    }

    @Override
    public void unknownElement(long position, VInteger identifier, VInteger size) {
        processElement(identifier, null, null, size);
    }

    @Override
    public void endFile() {
    }

    @Override
    public void error(Throwable e) {
    }

    @Override
    public void fatalError(Throwable e) {
    }

}
